//
//  SDShopDetailsViewController.m
//  Sydeny
//
//  Created by Rainbow on 7/26/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SDShopDetailsViewController.h"
#import "LoginViewController.h"
#import "MapViewController.h"
#import "CommentsTableViewController.h"
#import "PersistenceData.h"
#import "ITSActivityIndicator.h"
#import "SHKItem.h"
#import "SHKActionSheet.h"


@implementation SDShopDetailsViewController
@synthesize scrollView;
@synthesize nameLabel;
@synthesize introduceLabel;
@synthesize telephoneBtn;
@synthesize favorateBtn;
@synthesize shareBtn;

@synthesize descriptionBtn;
@synthesize commentsBtn;
@synthesize imageScrollView;

@synthesize upView;
@synthesize downView;

@synthesize shop;
@synthesize detailsTextView;
@synthesize commentTableView;

-(id)initWithShop:(Shop *)aShop
{
    self = [super init];
    if (self) {
        self.shop = aShop; 
    }
    return self;
}

- (IBAction)tapOnCommentBtn:(id)sender {
    CommentsTableViewController *commentsTableViewController = [[CommentsTableViewController alloc] initWithShopId:shop.shopId];
    [self.navigationController pushViewController:commentsTableViewController animated:YES];
    [commentsTableViewController release];
}

- (IBAction)tapOnPhoneBtn:(id)sender {
	UIWebView *webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
	[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", shop.telephone]]]];
}

- (IBAction)tapOnFavoriteBtn:(id)sender {
    BOOL isSuccess = [[PersistenceData sharedData] addShop:shop];
    if (isSuccess) {
        [[ITSActivityIndicator currentIndicator] displayCompleted:@"添加收藏成功！"];
    }
    else
    {
        [[ITSActivityIndicator currentIndicator] displayFailed:@"已经添加该收藏！"];
    }
}

- (IBAction)tapOnShareBtn:(id)sender {
    NSString *shareText = [NSString stringWithFormat:@"%@。 地址：%@ 电话：%@", shop.title, shop.address,shop.telephone];
	SHKItem *item = [SHKItem text:shareText];
	SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
	
	[actionSheet showFromTabBar:self.tabBarController.tabBar];
}

- (void)dealloc
{
    [nameLabel release];
    [introduceLabel release];
    [telephoneBtn release];
    [favorateBtn release];
    [shareBtn release];
    [descriptionBtn release];
    [commentsBtn release];
    [imageScrollView release];
    [shop release];
    [upView release];
    [downView release];
    [detailsTextView release];
    [commentTableView release];
    [scrollView release];
    [leftArrowImageView release];
    [rightArrowImageView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *logoView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]] autorelease];
    logoView.frame = CGRectMake(0, 0, 94, 29);
    self.navigationItem.titleView = logoView;
    
    UIButton *backBtn = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(tapBackBtn) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
    self.navigationItem.leftBarButtonItem = backItem;
    
    UIButton *rightBtn = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)] autorelease];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_btn.png"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(tapMapBtn) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"地图" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *rightItem = [[[UIBarButtonItem alloc] initWithCustomView:rightBtn] autorelease];
    self.navigationItem.rightBarButtonItem = rightItem;
     
    upView.layer.cornerRadius = 8;
    downView.layer.cornerRadius = 12;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"telephone_icon.png"]];
    imageView.frame = CGRectMake(0, 0, 12, 18);
    [telephoneBtn addSubview:imageView];
    [imageView release];
    
    
    [telephoneBtn setTitle:shop.telephone forState:UIControlStateNormal];
    
    nameLabel.text = shop.name;
    introduceLabel.text = shop.address;
    detailsTextView.text = shop.details;
    
    if (shop.images != nil&& [shop.images count] > 0) {
        
        imageScrollView = [[ImageScrollView alloc] initWithImages:shop.images];
        imageScrollView.frame = CGRectMake(0, 30, 300, 90);
        imageScrollView.delegate = self;
        imageScrollView.showsHorizontalScrollIndicator = NO;
        [downView addSubview:imageScrollView];
        
        leftArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"left_arrow.png"]];
        leftArrowImageView.frame = CGRectMake(-5, 65, 20, 20);
        leftArrowImageView.alpha = 0;
        [downView addSubview:leftArrowImageView];
        
        rightArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right_arrow.png"]];
        rightArrowImageView.frame = CGRectMake(285, 65, 20, 20);
        rightArrowImageView.alpha = 0;
        if (imageScrollView.contentSize.width > imageScrollView.contentOffset.x + 300) {
            rightArrowImageView.alpha = 1;
        }
        [downView addSubview:rightArrowImageView];
    }
    
    [downView addSubview:detailsTextView];
    
    CGRect downViewRect = downView.frame;
    
    if (shop.images != nil&& [shop.images count] > 0) {
        
        detailsTextView.frame = CGRectMake(0, 130, 300, detailsTextView.contentSize.height);
        downViewRect.size.height = 130+detailsTextView.contentSize.height;
        scrollView.contentSize = CGSizeMake(300, detailsTextView.contentSize.height + 310);
    }
    else
    {
        detailsTextView.frame = CGRectMake(0, 30, 300, detailsTextView.contentSize.height);
        scrollView.contentSize = CGSizeMake(300, detailsTextView.contentSize.height + 210);
    }
    
    downView.frame = downViewRect;
    
                                
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setDetailsTextView:nil];
    [self setCommentTableView:nil];
    [self setScrollView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (imageScrollView.contentOffset.x <= 0) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.5f];
        leftArrowImageView.alpha = 0;
        [UIView commitAnimations];
    }
    else
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.5f];
        leftArrowImageView.alpha = 1;
        [UIView commitAnimations];
    }
    
    if (imageScrollView.contentSize.width > imageScrollView.contentOffset.x + 300) {
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.5f];
        rightArrowImageView.alpha = 1;
        [UIView commitAnimations];
    }
    else
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.5f];
        rightArrowImageView.alpha = 0;
        [UIView commitAnimations];
    }
}

-(void)tapBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)tapMapBtn
{
    
    NSMutableArray *annotations = [[[NSMutableArray alloc] init] autorelease];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = [shop.latitude floatValue];
    coordinate.longitude = [shop.longtitude floatValue];
    ITSAnnotation *annotation = [[ITSAnnotation alloc] initWithCoordinate:coordinate];
    annotation.title = shop.name;
    annotation.subtitle = shop.address;
    [annotations addObject:annotation];
    [annotation release];
    
    MapViewController *mapViewController = [[MapViewController alloc] initWithAnnotation:annotations];
	[self.navigationController pushViewController:mapViewController animated:YES];
	[mapViewController release];
    
    
}


@end
